/**
 * The contents of this file are subject to the OpenMRS Public License
 * Version 1.0 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://license.openmrs.org
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 *
 * Copyright (C) OpenMRS, LLC.  All Rights Reserved.
 */
package org.openmrs.module.datalogging.handler.variable;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.openmrs.module.datalogging.enums.VariableType;

/**
 *
 */
public class VariableHandlerFactory {
	
	private static final Log log = LogFactory.getLog(VariableHandlerFactory.class);
	
	private static VariableHandlerFactory instance;
	
	private Map<VariableType, VariableHandler> variableHandlerMapping;
	
	private VariableHandlerFactory() {
		registerDefaultHandler();
	}
	
	/**
	 * @return
	 */
	private synchronized static VariableHandlerFactory getInstance() {
		if (instance == null)
			instance = new VariableHandlerFactory();
		return instance;
	}
	
	private void registerDefaultHandler() {
		if (log.isDebugEnabled())
			log.debug("Registering default handler ...");
		getComponentHandlerMapping().put(VariableType.CONSTANT_COMPONENT, new ConstantVariableHandler());
		getComponentHandlerMapping().put(VariableType.ENCOUNTER_COMPONENT, new EncounterVariableHandler());
		getComponentHandlerMapping().put(VariableType.OBS_COMPONENT, new ObsVariableHandler());
		getComponentHandlerMapping().put(VariableType.LOGIC_COMPONENT, new LogicVariableHandler());
	}
	
	/**
	 * Return the value of the variableHandlerMapping
	 * 
	 * @return the variableHandlerMapping
	 */
	private Map<VariableType, VariableHandler> getComponentHandlerMapping() {
		if (variableHandlerMapping == null)
			variableHandlerMapping = new HashMap<VariableType, VariableHandler>();
		return variableHandlerMapping;
	}
	
	/**
	 * Register a new handler to create Rule object
	 * 
	 * @param variableType the variable name
	 * @param handler the handler class
	 */
	public static void registerHandler(VariableType variableType, VariableHandler handler) {
		getInstance().getComponentHandlerMapping().put(variableType, handler);
	}
	
	/**
	 * Search for a variable handler in all registered handler
	 * 
	 * @param variableType the variable name
	 * @return handler of the variable or null if no handler is registered for the variable
	 */
	public static VariableHandler getHandler(VariableType variableType) {
		return getInstance().getComponentHandlerMapping().get(variableType);
	}
	
	/**
	 * Unregister a variable handler
	 * 
	 * @param variable the variable that will be unregistered
	 */
	public static void unregisterHandler(VariableType variable) {
		getInstance().getComponentHandlerMapping().remove(variable);
	}
	
}
